import sys
import fileinput

import argparse


parser = argparse.ArgumentParser()
parser.add_argument("textToReplace", help="text To Replace")
args = parser.parse_args()

textToReplace=args.textToReplace


out=textToReplace+"_grpc.cpp"
textToSearch="nnnnnnnnn"


with open(out, 'w') as f:
    print(
        '''


#include <iostream>

#include <map>
#include <memory>
#include <string>
#include <thread>

using namespace std;

#include "nnnnnnnnn_grpc.h"

class grpc_service_nnnnnnnnnImpl final
    : public nnnnnnnnn_pack::grpc_service_nnnnnnnnn::Service {
public:
  map<string, string> nnnnnnnnn_map;
  ::grpc::Status
  setnnnnnnnnn(::grpc::ServerContext *context,
               const ::nnnnnnnnn_pack::nnnnnnnnnSetRequest *request,
               ::nnnnnnnnn_pack::nnnnnnnnnSetReply *response) {
    auto key = request->nnnnnnnnn_request().nnnnnnnnn_key();
    auto value = request->nnnnnnnnn_request().nnnnnnnnn_value();
    cout << "receive set:" << endl;
    cout << "key:" << key << endl;
    cout << "value:" << value << endl;
    nnnnnnnnn_map[key] = value;
    response->set_nnnnnnnnn_key(key);
    response->set_flag(true);
    return Status::OK;
  };

  ::grpc::Status
  getnnnnnnnnn(::grpc::ServerContext *context,
               const ::nnnnnnnnn_pack::nnnnnnnnnGetRequest *request,
               ::nnnnnnnnn_pack::nnnnnnnnnGetReply *response) {
    auto key = request->nnnnnnnnn_key();
    cout << "receive get:"<< endl;
    cout << "key:" << key << endl;
    auto iter = nnnnnnnnn_map.find(key);
    if (iter != nnnnnnnnn_map.end()) {
      auto value = iter->second;
      cout << "value:" << value << endl;
      response->set_flag(true);

      ::nnnnnnnnn_pack::nnnnnnnnn *nnnnnnnnn = new ::nnnnnnnnn_pack::nnnnnnnnn;
      nnnnnnnnn->set_nnnnnnnnn_key(key);
      nnnnnnnnn->set_nnnnnnnnn_value(iter->second);

      response->set_allocated_nnnnnnnnn_reply(nnnnnnnnn);
    }

    return Status::OK;
  };
};

void RunServer() {
  std::string server_address("0.0.0.0:50051");
  grpc_service_nnnnnnnnnImpl service;

  ServerBuilder builder;
  builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  builder.RegisterService(&service);

  std::unique_ptr<Server> server(builder.BuildAndStart());
  std::cout << "Server listening on " << server_address << std::endl;

  server->Wait();
}

int main(int argc, char **argv) {
  RunServer();

  return 0;
}
''', file=f)
with fileinput.FileInput(out, inplace=True) as file:
    for line in file:
        print(line.replace(textToSearch, textToReplace), end='')